package com.cloudpark.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cloudpark.common.util.Constants;
import com.cloudpark.model.entity.AdminUser;
import com.cloudpark.model.entity.AdminUserRole;
import com.cloudpark.model.entity.Menu;
import com.cloudpark.model.entity.NavMenu;
import com.cloudpark.service.MenuService;
import com.cloudpark.service.UserService;

@Controller
public class NavMenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private UserService userService;

    /**
     * nav menu 鐐瑰嚮浜嬩欢澶勭悊
     * 
     * @param request
     * @param response
     */
    @RequestMapping(value = "/nav/menu", method = RequestMethod.GET)
    public void menu(HttpServletRequest request, HttpServletResponse response) {
        String navIdStr = request.getParameter("navId");
        Long navId = Long.valueOf(navIdStr);
        NavMenu navMenu = menuService.findNavMenuById(navId);
        String redirectUrl = "";
        if (navMenu.getHasMenu() == Constants.CONST_ONE) {
            AdminUser adminUser = (AdminUser) SecurityUtils.getSubject().getSession()
                    .getAttribute(Constants.SESSION_USER);
            List<AdminUserRole> userRoles = userService.findRolesByUserId(adminUser.getId());
            List<Long> roleIds = new ArrayList<Long>();
            List<Long> roleMenuIds = new ArrayList<Long>();
            for (AdminUserRole ur : userRoles) {
                roleIds.add(ur.getRoleId());
                roleMenuIds = menuService.findMenuIdsByRoleIds(roleIds);
            }
            // 鏌ユ壘nav涓嬮潰鐨勪竴绾ц彍锟�
            List<Menu> list = menuService.findMenusByNavId(navId);
            int i = 0;
            int j = 0;
            for (Menu root : list) {
                if (roleMenuIds.contains(root.getId())) { // 鏉冮檺绛涳拷?
                    if (root.getIsLeaf() == Constants.CONST_ONE) {
                        if (i == 0 && j == 0) {
                            redirectUrl = root.getUrl() + "?navId=" + root.getNavId();
                        }
                        j++;
                    } else if (root.getIsLeaf() == Constants.CONST_TWO) {
                        List<Menu> children = menuService.findMenusByParentId(root.getId());
                        int k = 0;
                        for (Menu child : children) {
                            if (roleMenuIds.contains(child.getId())) { // 鏉冮檺绛涳拷?
                                if (i == 0 && k == 0) {
                                    redirectUrl = child.getUrl() + "?navId=" + child.getNavId() + "&menuId="
                                            + child.getId();
                                }
                                k++;
                            }
                        }
                    }
                    i++;
                }
            }
        } else if (navMenu.getHasMenu() == Constants.CONST_TWO) {
            redirectUrl = navMenu.getUrl();
        }
        String url = Constants.baseUrl + redirectUrl;
        try {
            WebUtils.issueRedirect(request, response, url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
